﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent (typeof(Animator))]
public class playAni : MonoBehaviour
{
	Animator ani;
	// Use this for initialization
	void Start ()
	{
		this.ani = this.GetComponent<Animator> ();
	}

	public string state = null;
	public float length = 0;

	public void SetState (string name)
	{

		if (this.ani == null) {
			this.ani = this.GetComponent<Animator> ();
		}
		state = name;
		if (string.IsNullOrEmpty (name))
			return;
		if (this.length <= 0) {		
			this.ani.Play (name, 0, 0);
			this.ani.Update (0);
		}
	}

	public void PlayOnce (string name)
	{
		this.ani.Play (name, 0, 0);
		this.ani.Update (0);
		var infos = this.ani.GetCurrentAnimatorClipInfo (0);
		foreach (var i in infos) {
			if (i.clip.name == name) {
				length = i.clip.length;
			}
		}
	}

	// Update is called once per frame
	void Update ()
	{
		if (length > 0) {
			length -= Time.deltaTime;
			if (length <= 0) {
				if (string.IsNullOrEmpty (state))
					return;
				this.ani.Play (state, 0, 0);
				this.ani.Update (0);
			}
		}


	}
}
